<template>
  <div>
    <a-button type="primary" @click="show = true">
      添加任务
    </a-button>
    <a-modal v-model="show" title="添加任务" :width="700">
      <template slot="footer">
        <a-button key="back" @click="show = false">
          取消
        </a-button>
        <a-button key="submit" type="primary" :loading="loading" @click="handleOk">
          提交
        </a-button>
      </template>
      <a-form-model :model="form" :label-col="labelCol" :wrapper-col="wrapperCol">
        <a-form-model-item label="任务名称">
          <a-input v-model="form.name" />
        </a-form-model-item>
        <a-form-model-item label="选择账户">
          <a-select
            mode="multiple"
            :maxTagCount="2"
            v-model="accountArr"
            style="width: 100%;"
            @change="accountSelect"
            @search="accountSelectSearch"
          >
            <a-select-option :value="item.account" v-for="(item, index) in accountList" :key="index">
              {{ item.account }}
            </a-select-option>
          </a-select>
        </a-form-model-item>
      </a-form-model>
    </a-modal>
  </div>
</template>

<script>
import { AitaskaddTask } from '@/api/dianshang/dianshang'
import { getAllBdAccountList } from '@/api/xinxiliu/account'

export default {
  data() {
    return {
      show: false,
      labelCol: { span: 4 },
      wrapperCol: { span: 14 },
      form: {
        name: '',
        account:'',
      },
      accountArr: [],
      accountList: [],
      totalAccountList: [],
      loading: false,
    }
  },
  methods: {
    handleOk() {
      const form = this.form
      this.loading = true
      AitaskaddTask(form).then((res) => {
        this.loading = false
        if (res.errcode == 0) {
          this.$message.success('新增成功')
          this.show = false
          this.$emit('confirm')
        }
      }).catch(() => {
        this.loading = false
      });
    },
    getAllBdAccountList() {
      // 获取所有账户
      getAllBdAccountList({}).then((res) => {
        if (res.errcode == 0) {
          let accountList = res.data.filter(e => e.pid != 0)
          this.totalAccountList = accountList
          this.accountList = accountList.slice(0, 100)
        }
      }).catch(() => {
        
      });
    },
    accountSelect() {
      this.form.account = this.accountArr.join(',')
    },
    accountSelectSearch(value) {
      if (value != '') {
        this.accountList = this.totalAccountList.filter(e=>e.account.indexOf(value) >= 0).slice(0, 100)
      } else {
        this.accountList = this.totalAccountList.slice(0, 100)
      }
    },
  },
  watch: {
    show(val) {
      if (val) {
        this.getAllBdAccountList()
      }
    }
  }
}
</script>